var t = getApp(), a = require("../../utils/util.js"), s = null, e = null, i = null, n = 0;

Page({
    data: {
        showSetting: !0,
        canUserSetMs: !1,
        videoAdLoading: !1,
        status: {
            curMs: 200,
            max: 1e3,
            min: 100,
            num: 2,
            pageSum: [ 1, 2, 3, 4, 5, 6 ],
            curShape: "横"
        },
        shapes: [ "横", "竖", "扇形", "旋转" ],
        ledColor: [ "#000000", "#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#00FFFF", "#FF00FF", "#FFFFFF" ],
        startTimeDo: !1
    },
    onLoad: function() {
        s = wx.getSystemInfoSync(), e = wx.createCanvasContext("ledCanvas");
    },
    videoAdLoad: function() {
        var a = this;
        if (1 != this.data.videoAdLoading) return null != i ? (this.setData({
            videoAdLoading: !0
        }), this.videoAdShow()) : void (wx.createRewardedVideoAd && (this.setData({
            videoAdLoading: !0
        }), (i = wx.createRewardedVideoAd({
            adUnitId: t.ad.jlAd_03,
            multiton: !0
        })).onLoad(function() {
            1 == a.data.videoAdLoading && a.videoAdShow();
        }), i.onError(function(t) {
            i = null, a.setData({
                videoAdLoading: !1
            });
        }), i.onClose(function(t) {
            a.setData({
                videoAdLoading: !1
            }), t && t.isEnded && (a.setData({
                canUserSetMs: !0
            }), wx.setStorageSync("canUserSetLedData", {
                canUserSetMs: !0,
                time: Date.parse(new Date())
            }));
        })));
    },
    onShow: function() {
        try {
            var t = wx.getStorageSync("statusLedCache");
            t && this.setData({
                status: t
            });
        } catch (t) {}
        this.canUserSetMsHandle();
    },
    onUnload: function() {
        this.stopTime();
    },
    onShareAppMessage: function() {},
    onStartLight: function() {
        this.startTime();
    },
    showModal: function(t) {
        this.setData({
            showSetting: !0,
            startTimeDo: !1
        });
    },
    hideModal: function() {
        wx.setStorageSync("statusLedCache", this.data.status), this.setData({
            showSetting: !1
        });
    },
    numChange: function(t) {
        this.setData({
            "status.num": Number(t.detail.value)
        }), wx.setStorageSync("statusLedCache", this.data.status);
    },
    shapeChange: function(t) {
        var a = this, s = a.data.status.num;
        a.data.shapes.indexOf(t.detail.value) >= 4 ? a.data.canUserSetMs ? (s = s < 3 ? 3 : s, 
        this.setData({
            "status.curShape": t.detail.value,
            "status.pageSum": [ 3, 4, 5, 6, 7, 8 ],
            "status.num": s
        })) : (a.setData({
            "status.curShape": "横"
        }), wx.showModal({
            title: "气氛灯",
            content: "扇形、旋转\n\r\n\r需要观看广告后才可以设置哦",
            confirmText: a.data.videoAdLoading ? "加载中" : "立即观看",
            showCancel: !a.data.videoAdLoading,
            cancelText: "取消",
            success: function(t) {
                t.confirm && a.videoAdLoad();
            }
        })) : (s = s > 6 ? 6 : s, this.setData({
            "status.curShape": t.detail.value,
            "status.pageSum": [ 1, 2, 3, 4, 5, 6 ],
            "status.num": s
        })), wx.setStorageSync("statusLedCache", this.data.status);
    },
    catchtap: function() {},
    canvasTouchStart: function() {
        this.showModal();
    },
    startTime: function() {
        this.data.startTimeDo ? this.stopTime() : (this.hideModal(), this.setData({
            startTimeDo: !0
        }), n = 0, this.setCountDown());
    },
    stopTime: function() {
        this.setData({
            startTimeDo: !1
        });
    },
    setCountDown: function() {
        var t = this.data.status.curMs;
        this.data.startTimeDo && (this.drawCanvas(), setTimeout(this.setCountDown, t));
    },
    drawCanvas: function() {
        for (var t = this.data.status.curShape, a = this.data.shapes, i = 0; i < this.data.status.num; i++) {
            var o = (n + i) % this.data.ledColor.length;
            if (t == a[0]) e.fillStyle = this.data.ledColor[o], e.fillRect(0, s.windowHeight / this.data.status.num * i, s.windowWidth, s.windowHeight / this.data.status.num); else if (t == a[1]) e.fillStyle = this.data.ledColor[o], 
            e.fillRect(s.windowWidth / this.data.status.num * i, 0, s.windowWidth / this.data.status.num, s.windowHeight); else if (t == a[2] || t == a[3]) {
                var d = 2 * Math.PI / this.data.status.num, u = s.windowWidth / 2, h = s.windowHeight / 2, c = d * i;
                e.beginPath(), e.strokeStyle = this.data.ledColor[o], e.moveTo(u, h), t == a[3] && (c += (n + c) % d), 
                e.arc(u, h, u + h, c, c + d), e.stroke(), e.fillStyle = this.data.ledColor[o], e.fill();
            }
        }
        n++, e.draw();
    },
    msChange: function(t) {
        this.setData({
            "status.curMs": t.detail.value
        }), wx.setStorageSync("statusLedCache", this.data.status);
    },
    videoAdShow: function() {
        var t = this;
        i && i.show().catch(function() {
            t.setData({
                videoAdLoading: !1
            }), i.load().then(function() {
                return i.show();
            }).catch(function(t) {
                a.videoAdErrCodeHandle(t);
            });
        });
    },
    canUserSetMsHandle: function() {
        try {
            var t = wx.getStorageSync("canUserSetLedData");
            "" != t && ((Date.parse(new Date()) - t.time) / 1e3 < 43200 ? this.setData({
                canUserSetMs: !0
            }) : (this.setData({
                "status.curShape": "横"
            }), wx.setStorageSync("statusLedCache", this.data.status)));
        } catch (t) {}
    }
});